/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.lanchimage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.lanchimage.LanchImage;
import com.server.modules.sys.service.lanchimage.LanchImageService;

/**
 * 启动页图片管理Controller
 * @author 王磊
 * @version 2016-09-05
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/lanchimage/lanchImage")
public class LanchImageController extends BaseController {

	@Autowired
	private LanchImageService lanchImageService;
	
	@ModelAttribute
	public LanchImage get(@RequestParam(required=false) String id) {
		LanchImage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = lanchImageService.get(id);
		}
		if (entity == null){
			entity = new LanchImage();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:lanchimage:lanchImage:view")
	@RequestMapping(value = {"list", ""})
	public String list(LanchImage lanchImage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LanchImage> page = lanchImageService.findPage(new Page<LanchImage>(request, response), lanchImage); 
		model.addAttribute("page", page);
		model.addAttribute("lanchImage", lanchImage);
		return "modules/sys/lanchimage/lanchImageList";
	}

	@RequiresPermissions("sys:lanchimage:lanchImage:view")
	@RequestMapping(value = "form")
	public String form(LanchImage lanchImage, Model model) {
		model.addAttribute("lanchImage", lanchImage);
		return "modules/sys/lanchimage/lanchImageForm";
	}

	@RequiresPermissions("sys:lanchimage:lanchImage:edit")
	@RequestMapping(value = "save")
	public String save(LanchImage lanchImage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, lanchImage)){
			return form(lanchImage, model);
		}
		String url = StringUtils.getFirstUrl(lanchImage.getUrl());//过滤竖线
		lanchImage.setUrl(url);
		lanchImageService.save(lanchImage);
		addMessage(redirectAttributes, "保存启动图成功");
		return "redirect:"+Global.getAdminPath()+"/sys/lanchimage/lanchImage/?repage";
	}
	
	@RequiresPermissions("sys:lanchimage:lanchImage:edit")
	@RequestMapping(value = "delete")
	public String delete(LanchImage lanchImage, RedirectAttributes redirectAttributes) {
		lanchImageService.delete(lanchImage);
		addMessage(redirectAttributes, "删除启动图成功");
		return "redirect:"+Global.getAdminPath()+"/sys/lanchimage/lanchImage/?repage";
	}

}